sysdir set PERSONAL "I:\MAA2015-20 Credit and Education\stata"
do "I:\MAA2015-20 Credit and Education\stata\outreg2.ado"
do "I:\MAA2015-20 Credit and Education\stata\rdrobust.ado"
do "I:\MAA2015-20 Credit and Education\stata\rdplot.ado"


*Appendix Table A1
use "basesample_earn.dta",clear
set more off
set matsize 10000
cumul ratio_pass if ratio_pass>=0.4&ratio_pass<=0.6 ,gen(cdf1)
cumul ratio_pass if female==1&ratio_pass>=0.4&ratio_pass<=0.6 ,gen(cdf_female1)

cumul ratio_pass if ratio_pass>=0.35&ratio_pass<=0.65,gen(cdf2)
cumul ratio_pass if female==1&ratio_pass>=0.35&ratio_pass<=0.65,gen(cdf_female2)

cumul ratio_pass if ratio_pass>=0.3 &ratio_pass<=0.7,gen(cdf3)
cumul ratio_pass if female==1&ratio_pass>=0.3 &ratio_pass<=0.7,gen(cdf_female3)

rdrobust cdf1  ratio_pass , c(0.5) h(0.10) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf2 ratio_pass , c(0.5) h(0.15) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf3 ratio_pass , c(0.5) h(0.20) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf_female1 ratio_pass if female==1 , c(0.5) h(0.10) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf_female2 ratio_pass if female==1 , c(0.5) h(0.15) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf_female3 ratio_pass if female==1 , c(0.5) h(0.20) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

use "basesample_earn_08.dta",clear
set more off
set matsize 10000
cumul ratio_pass if ratio_pass>=0.4&ratio_pass<=0.6 ,gen(cdf1)
cumul ratio_pass if female==1&ratio_pass>=0.4&ratio_pass<=0.6 ,gen(cdf_female1)

cumul ratio_pass if ratio_pass>=0.35&ratio_pass<=0.65,gen(cdf2)
cumul ratio_pass if female==1&ratio_pass>=0.35&ratio_pass<=0.65,gen(cdf_female2)

cumul ratio_pass if ratio_pass>=0.3 &ratio_pass<=0.7,gen(cdf3)
cumul ratio_pass if female==1&ratio_pass>=0.3 &ratio_pass<=0.7,gen(cdf_female3)

rdrobust cdf1  ratio_pass , c(0.5) h(0.10) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf2 ratio_pass , c(0.5) h(0.15) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf3 ratio_pass , c(0.5) h(0.20) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf_female1 ratio_pass if female==1 , c(0.5) h(0.10) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf_female2 ratio_pass if female==1 , c(0.5) h(0.15) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 

rdrobust cdf_female3 ratio_pass if female==1 , c(0.5) h(0.20) fuzzy(treat)   
outreg2 using tableA1,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 


*Appendix Table A2
use "basesample_earn.dta",clear
set more off
set matsize 10000

foreach y of varlist age female maori  white disable full uni business science loan_amt decile percentile {

rdrobust `y' ratio_pass , c(0.5) h(0.15) b(0.3) fuzzy(treat)   
outreg2 using tableA2,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 
}

foreach y of varlist age  maori  white disable full uni business science loan_amt decile percentile{

rdrobust `y' ratio_pass if female == 1, c(0.5) h(0.15) b(0.3) fuzzy(treat)   
outreg2 using tableA2,excel dec(3) adds(bw obs l, e(N_h_l), bw obs r,e(N_h_r)) 
}

*Figure A1
*******************************************************************************
*We use the rdplot commands to create cell counts and "cell.dta"
/*
use "basesample_earn.dta",clear
set more off
set matsize 10000
replace ratio_pass=0 if ratio_pass<0.06
rdplot loan ratio_pass,nbins(25 25) h(0.15) b(0.3) p(1)  c(0.5) generate(cell) graph_options(ylabel(0(.2)1)xtitle("Pass Rates") graphregion(color(white))  title("Loan"))
tab cell
*Copy the number of observations in each cell and paste to create cell.dta
*We call the variable "density" 

use "basesample_earn_08.dta",clear
set more off
set matsize 10000
rdplot loan ratio_pass ,nbins(25 25) h(0.15) b(0.3) p(1)  c(0.5) generate(cell)  graph_options(ylabel(0(.2)1)xtitle("Pass Rates") graphregion(color(white))  title("Loan"))
tab cell 
*Copy the number of observations in each cell and paste to create cell.dta
*We call the variable "density08" 
*Notice that all numbers of observations need to randomly rounded to base 3

replace density=(density/49386)*100
replace density08 =(density08/50988)*100
*All counts are randomly rounded to base 3
save "cell.dta",replace
*/
********************************************************************************
use "cell.dta",clear

 
twoway (bar density  ratio_pass, title(Analysis Sample) barwidth(.02) bcolor(khaki) blcolor(navy) yscale(range(0 10)) ylabel(0(2)10) xlabel(.0(.2)1) xline(.5) legend(off) graphregion(color(white))) if ratio_pass<.99
graph save "Graph" "FigureA1a.gph", replace
graph export "FigureA1a.png", as(png) name("Graph") replace
 
twoway (bar density08 ratio_pass, title(2001-2005 Student Cohorts) barwidth(.02) bcolor(khaki) blcolor(navy) yscale(range(0 10)) ylabel(0(2)10) xlabel(.0(.2)1) xline(.5) legend(off) graphregion(color(white))) if ratio_pass<.99
graph save "Graph" "FigureA1b.gph", replace
graph export "FigureA1b.png", as(png) name("Graph") replace

*Figure A2
*******************************************************************************
*We use the rdplot commands to create cell counts by gender and "cell_sex.dta"
/*
use "basesample_earn.dta",clear
set more off
set matsize 10000
replace ratio_pass=0 if ratio_pass<0.05
preserve
replace ratio_pass=0.0999 if ratio_pass>=0.1&ratio_pass<0.125
rdplot loan ratio_pass if female==1,nbins(20 25 h(0.15) b(0.3) p(1)  c(0.5) generate(cell_female) graph_options(ylabel(0(.2)1)xtitle("Pass Rates") graphregion(color(white))  title("Loan (Female)"))
tab cell_female
restore
rdplot loan ratio_pass if female==0,nbins(20 25) h(0.15) b(0.3) p(1)  c(0.5) generate(cell_male) graph_options(ylabel(0(.2)1)xtitle("Pass Rates") graphregion(color(white))  title("Loan (Male)"))
tab cell_male

use "basesample_earn_08.dta",clear
set more off
set matsize 10000
preserve
replace ratio_pass=0 if ratio_pass<0.05
rdplot loan ratio_pass if female==1,nbins(20 25) h(0.15) b(0.3) p(1)  c(0.5) generate(cell_female)  graph_options(ylabel(0(.2)1)xtitle("Pass Rates") graphregion(color(white))  title("Loan (2001-2005 Female)"))
tab cell_female
restore
rdplot loan ratio_pass if female==0,nbins(20 25) h(0.15) b(0.3) p(1)  c(0.5) generate(cell_male)   graph_options(ylabel(0(.2)1) xtitle("Pass Rates") graphregion(color(white)) title("Loan (2001-2005 Male)"))
tab cell_male

*Copy the number of observations in each cell and paste to create cell_sex.dta
*All numbers of observations need to randomly rounded to base 3

replace density_female=(density_female/28701)*100
replace density_male=(density_male/20676)*100
replace density08_female=(density08_female/29652)*100
replace density08_male=(density08_male/21333)*100
save "cell_sex.dta",replace
*/
********************************************************************************

use "cell_sex.dta",clear

twoway (bar density_female ratio_pass if ratio_pass<.5, barwidth(.025)bcolor(khaki) blcolor(navy) ) (bar density_female ratio_pass if ratio_pass>=.5 ,barwidth(.02) bcolor(khaki) blcolor(navy) yscale(range(0 10)) ylabel(0(2)10) xlabel(.0(.2)1) xline(.5) legend(off) graphregion(color(white)) title(Analysis Sample (Female))) if ratio_pass<.99
graph save "Graph" "FigureA2a_female.gph", replace
graph export "FigureA2a_female.png", as(png) name("Graph") replace

twoway (bar density_male ratio_pass if ratio_pass<.5, barwidth(.025)bcolor(khaki) blcolor(navy) ) (bar density_male ratio_pass if ratio_pass>=.5 ,barwidth(.02) bcolor(khaki) blcolor(navy) yscale(range(0 10)) ylabel(0(2)10) xlabel(.0(.2)1) xline(.5) legend(off) graphregion(color(white)) title(Analysis Sample (Male))) if ratio_pass<.99
graph save "Graph" "FigureA2a_male.gph", replace
graph export "FigureA2a_male.png", as(png) name("Graph") replace

twoway (bar density08_female ratio_pass if ratio_pass<.5, barwidth(.025)bcolor(khaki) blcolor(navy) ) (bar density08_female ratio_pass if ratio_pass>=.5 ,barwidth(.02) bcolor(khaki) blcolor(navy) yscale(range(0 10)) ylabel(0(2)10) xlabel(.0(.2)1) xline(.5) legend(off) graphregion(color(white)) title(2001-2005 Studnet Cohorts (Female))) if ratio_pass<.99
graph save "FigureA2b_female.gph", replace
graph export "FigureA2b_female.png", as(png)  replace

twoway (bar density08_male ratio_pass if ratio_pass<.5, barwidth(.025)bcolor(khaki) blcolor(navy) ) (bar density08_male ratio_pass if ratio_pass>=.5 ,barwidth(.02) bcolor(khaki) blcolor(navy) yscale(range(0 10)) ylabel(0(2)10) xlabel(.0(.2)1) xline(.5) legend(off) graphregion(color(white)) title(2001-2005 Studnet Cohorts (Male))) if ratio_pass<.99
graph save  "FigureA2b_male.gph", replace
graph export "FigureA2b_male.png", as(png) replace

*Figure A3
use "basesample_earn.dta",clear
set more off
set matsize 10000
replace ratio_pass=0 if ratio_pass<0.06

rdplot female ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Female",size(large)) xtitle("Pass Rates"))
graph save Graph "female.gph", replace
graph export "female.png", as(png) replace

rdplot age ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(18(1)23) title("Age",size(large)) xtitle("Pass Rates"))
graph save Graph "age.gph", replace
graph export "age.png", as(png) replace

rdplot white ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("European",size(large)) xtitle("Pass Rates"))
graph save Graph "white.gph", replace
graph export "white.png", as(png) replace

rdplot maori ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Māori and Pasifika",size(large)) xtitle("Pass Rates"))
graph save Graph "maori.gph", replace
graph export "maori.png", as(png) replace

rdplot loan_amt ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(6000(2000)14000) title("Borrowed Loan Amount",size(large)) xtitle("Pass Rates"))
graph save Graph "loanamt.gph", replace
graph export "loanamt.png", as(png) replace

rdplot disable ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Disability",size(large)) xtitle("Pass Rates"))
graph save Graph "disable.gph", replace
graph export "disable.png", as(png) replace

rdplot uni ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("University Students",size(large)) xtitle("Pass Rates"))
graph save Graph "uni.gph", replace
graph export "uni.png", as(png) replace

rdplot full ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Studying Full time and Full year",size(large)) xtitle("Pass Rates"))
graph save Graph "full.gph", replace
graph export "full.png", as(png) replace

rdplot science ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Science Major",size(large)) xtitle("Pass Rates"))
graph save Graph "science.gph", replace
graph export "science.png", as(png) replace

rdplot business ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Business Major",size(large)) xtitle("Pass Rates"))
graph save Graph "business.gph", replace
graph export "business.png", as(png) replace

rdplot decile ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(2)10) title("High School Deciles",size(large)) xtitle("Pass Rates"))
graph save Graph "decile.gph", replace
graph export "decile.png", as(png) replace

rdplot percentile ratio_pass ,nbins(25 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("NCEA Level 3 Percentile",size(large)) xtitle("Pass Rates"))
graph save Graph "percent.gph", replace
graph export "percent.png", as(png) replace

*Figure A4
use "basesample_earn.dta",clear
keep if female==1
replace ratio_pass=0 if ratio_pass<0.05
replace ratio_pass=0.0999 if ratio_pass>=0.1&ratio_pass<0.125

rdplot age ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(18(1)23) title("Age (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "age_female.gph", replace
graph export "age_female.png", as(png) replace

rdplot white ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("European (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "white_female.gph", replace
graph export "white_female.png", as(png) replace

rdplot maori ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Māori and Pasifika (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "maori_female.gph", replace
graph export "maori_female.png", as(png) replace

rdplot loan_amt ratio_pass ,nbins(20 25)  h(0.15) p(1) c(0.5) graph_options(ylabel(6000(2000)14000) title("Borrowed Loan Amount (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "loanamt_female.gph", replace
graph export "loanamt_female.png", as(png) replace

rdplot disable ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Disability (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "disable_female.gph", replace
graph export "disable_female.png", as(png) replace

rdplot uni ratio_pass ,nbins(20 25)  h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("University Students (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "uni_female.gph", replace
graph export "uni_female.png", as(png) replace

rdplot full ratio_pass ,nbins(20 25)  h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Studying Full time and Full year (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "full_female.gph", replace
graph export "full_female.png", as(png) replace

rdplot science ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Science Major (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "science_female.gph", replace
graph export "science_female.png", as(png) replace

rdplot business ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("Business Major (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "business_female.gph", replace
graph export "business_female.png", as(png) replace

rdplot decile ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(2)10) title("High School Deciles (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "decile_female.gph", replace
graph export "decile_female.png", as(png) replace

rdplot percentile ratio_pass ,nbins(20 25) h(0.15) p(1) c(0.5) graph_options(ylabel(0(.2)1) title("NCEA Level 3 Percentile (Female)",size(large)) xtitle("Pass Rates"))
graph save Graph "percent_female.gph", replace
graph export "percent_female.png", as(png) replace
